package com.gsmvn.ngamgaixinh;

import java.util.ArrayList;

import com.gsmvn.ngamgaixinh.SqlDataBaseHelper.CheckNewPhotoTable;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DataSource {
	private SQLiteDatabase database;
	public SQLiteDatabase getDatabase() {
		return database;
	}

	public void setDatabase(SQLiteDatabase database) {
		this.database = database;
	}

	private SqlDataBaseHelper dbHelper;

	public DataSource(Context context) {
		dbHelper = new SqlDataBaseHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public boolean isOpen() {
		if (database != null) {
			if (database.isOpen()) {
				return true;
			}
		}

		return false;
	}

	public void insert(String id) {
		Cursor cursor = database.rawQuery(
				"select * from favorite where photo_id = '" + id + "'", null);
		if (cursor.getCount() == 0) {
			ContentValues vl = new ContentValues();
			vl.put("photo_id", id);
			Log.e("", "insert to database id " + id);
			long d = database.insert("favorite", null, vl);
			Log.e("", "insert to database id count" + d);
		}
		cursor.close();

	}

	public boolean isAvailable(String id) {
		Cursor cursor = database.rawQuery(
				"select * from favorite where photo_id = '" + id + "'", null);
		if (cursor.getCount() == 0) {
			cursor.close();
			return false;
		}
		cursor.close();
		return true;
	}

	public void delete(String id) {
		ContentValues vl = new ContentValues();
		vl.put("photo_id", id);
		database.delete("favorite", "photo_id='" + id + "'", null);
	}

	public ArrayList<String> getList() {
		ArrayList<String> list = new ArrayList<String>();
		Cursor cursor = database.rawQuery("select * from favorite", null);
		if (cursor.moveToFirst()) {
			do {
				String id = cursor.getString(1);
				list.add(id);
			} while (cursor.moveToNext());

		}
		cursor.close();
		Log.d("", "list " + list.size());
		return list;
	}
	
	public void insertBefore(String Before){
		ContentValues vl = new ContentValues();
		vl.put(CheckNewPhotoTable.Column.Create_time, Before);
		Log.e(Before, "insert time "+Before);
		database.insert(CheckNewPhotoTable.NAME, null, vl);
	}
	
	public void updateBefore(String old, String Before){
		ContentValues vl = new ContentValues();
		vl.put(CheckNewPhotoTable.Column.Create_time, Before);
		database.update(CheckNewPhotoTable.NAME, vl, CheckNewPhotoTable.Column.Create_time+"='"+old+"'", null);
	}
	
	public String checkAfter(){
		Cursor c = database.rawQuery("select * from "+CheckNewPhotoTable.NAME, null);
		if(c.moveToFirst()){
			String after = c.getString(c.getColumnIndex(CheckNewPhotoTable.Column.Create_time));
			return after;
		}
		return null;
	}
	
	public Cursor checkCursorAfter(){
		Cursor c = database.rawQuery("select * from "+CheckNewPhotoTable.NAME, null);
		return c;
	}
}
